package com.fand.manager.controller;


import java.util.List;

import javax.annotation.Resource;

import com.fand.common.util.PageResult;
import com.fand.common.util.ResultHelp;
import com.fand.manager.model.menu.IndexMenuDTO;
import com.fand.manager.model.menu.SysMenuDeleteDTO;
import com.fand.manager.model.menu.SysMenuPostDTO;
import com.fand.manager.model.menu.SysMenuReturnDTO;
import com.fand.manager.model.menu.SysPMenuReturnDTO;
import com.fand.manager.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author javaboy
 * @since 2021-06-07
 */
@Api(tags = "菜单")
@RestController
@RequestMapping("/sys-menu")
public class SysMenuController {

    @Resource
    private ISysMenuService sysMenuService;

    @ApiOperation("保存菜单")
    @PreAuthorize("hasAuthority('sys:menu:add')")
    @PostMapping("/save")
    public ResultHelp<Boolean> saveMenu(@RequestBody SysMenuPostDTO sysMenuPostDTO){
        return ResultHelp.success(sysMenuService.saveMenu(sysMenuPostDTO));
    }

    @ApiOperation("菜单分页查询")
    @PreAuthorize("hasAuthority('sys:menu:query')")
    @GetMapping("/list")
    public PageResult<List<SysMenuReturnDTO>> listMenus(){
        return sysMenuService.listMenus();
    }

    @ApiOperation("批量删除菜单")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    @PostMapping("/deleteByIds")
    public ResultHelp<Boolean> deleteByIds(@RequestBody SysMenuDeleteDTO sysMenuDeleteDTO){
        return ResultHelp.success(sysMenuService.deleteByIds(sysMenuDeleteDTO.getMenuIds()));
    }

    @ApiOperation("查询父菜单")
    @PreAuthorize("hasAuthority('sys:menu:query')")
    @GetMapping("/findPMenu")
    public ResultHelp<SysPMenuReturnDTO> findPMenu(@RequestParam Long pid){
        return ResultHelp.success(sysMenuService.findPMenu(pid));
    }

    @ApiOperation("查询菜单")
    @PreAuthorize("hasAuthority('sys:menu:query')")
    @GetMapping("/findMenu")
    public ResultHelp<SysPMenuReturnDTO> findMenu(@RequestParam Long id){
        return ResultHelp.success(sysMenuService.findMenu(id));
    }

    @ApiOperation("修改菜单")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    @PostMapping("/update")
    public ResultHelp<Boolean> updateMenu(@RequestBody SysMenuPostDTO sysMenuPostDTO){
        return ResultHelp.success(sysMenuService.updateMenu(sysMenuPostDTO));
    }

    @ApiOperation("删除菜单及其子菜单")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    @PostMapping("/deleteById")
    public ResultHelp<Boolean> deleteById(@RequestBody SysMenuDeleteDTO sysMenuDeleteDTO){
        return ResultHelp.success(sysMenuService.deleteById(sysMenuDeleteDTO));
    }

    @ApiOperation("根据pid查询菜单")
    @PreAuthorize("hasAuthority('sys:menu:query')")
    @GetMapping("/findMenuByPid")
    public ResultHelp<List<SysMenuReturnDTO>> findMenuByPid(@RequestParam Long pid){
        return ResultHelp.success(sysMenuService.findMenuByPid(pid));
    }

    @ApiOperation("首页菜单项")
    @GetMapping("/indexMenu")
    public IndexMenuDTO indexMenu(){
        return sysMenuService.indexMenu();
    }


}

